import { EventBus } from '@/store/index'
import BaseCreatejs from '@/components/games/createjs/js/BaseCreatejs.js'
import { isJSON, getCompIdByParam } from '@/plugins/utils'

export default class FoodMap extends BaseCreatejs {
  constructor() {
    super();

    this.mapNames = ['beijing', 'anhui', 'chongqing', 'fujian', 'gansu', 'guangdong', 'guangxi', 'guizhou', 'henan', 'hebei', 'hainan', 'heilongjiang', 'hubei', 'hunan', 'jilin', 'jiangsu', 'liaoning', 'neimenggu', 'ningxia', 'qinghai', 'shandong', 'shanxi', 'shanxi_1', 'shanghai', 'sichuan', 'taiwan', 'tianjin', 'xizang', 'xianggang', 'aomen', 'xinjiang', 'yunnan', 'zhejiang', 'jiangxi'];
    // this.mapNames_ = ['北京','安徽','重庆','福建','甘肃','广东','广西','贵州','河南','河北',                               '海南','黑龙江','湖北','湖南','吉林',               '江苏','辽宁','内蒙古','宁夏','青海','山东','山西','陕西',                                '上海','四川','台湾','天津','西藏','香港','澳门','新疆','云南','浙江','江西'];
    this.itemFoods = {};
    this.items = {};
    this.mapNameObjs = {};
    this.urls = null;
  }

  initView() {
    let params = this.item.commonAttr.params;
    // {"operate":"2000","levelColors":{"1000":"80,50","2000":"180,100","3000":"280,200"},"popId":1,"imgCompId":"","listCompId":""} 站点1228
    if (params && isJSON(params)) {
      this.injectJsClassObj = JSON.parse(params);
    }

    let item;
    let itemFood;
    let mapName;
    for (let i = 0, len = this.mapNames.length; i < len; i++) {
      mapName = this.mapNames[i];
      this.mapNameObjs[mapName] = i;
      item = this.stagePage[mapName];
      item.mapName = mapName;
      item.mouseChildren = false;
      item.alpha = 0.01;
      item.addEventListener('click', (e) => {
        let itemTarget = e.target;
        //触发弹层
        let shareData = { url: this.urls[this.mapNameObjs[itemTarget.mapName]].url, content: itemTarget.num };
        this.store.commit('updateShareData', shareData);
        this.showPop(this.injectJsClassObj.popId);
        // this.mcCallback({
        //   isShowPop: {
        //     num: itemTarget.num,
        //     url: this.urls[this.mapNameObjs[itemTarget.mapName]].url,
        //   }
        // });
      });
      this.items[mapName] = item;

      itemFood = this.stagePage['food_' + this.mapNames[i]];
      itemFood.visible = false;
      itemFood.mouseChildren = false;
      this.itemFoods[mapName] = itemFood;
    }

    EventBus.$on('refreshDynamicData', () => {
      this.refreshMap();
    });
    this.refreshMap();
  }
  
  refreshMap() {
    let mapNameNumObj = {};
    let relateListId = getCompIdByParam(this.injectJsClassObj.listCompId);//通用列表组件id
    let relatePopImgId = getCompIdByParam(this.injectJsClassObj.imgCompId);//关联的弹层中动态图片组件id
    this.urls = this.componentMap[relatePopImgId].lists;
    const relateCompData = this.componentMap[relateListId];
    let lists = relateCompData.lists;
    _.forEach(lists, (item) => {
      let index = item.voteId - 1;
      let mapName = this.mapNames[index];
      this.items[mapName].num = item.num;
      if (item.num >= this.injectJsClassObj.operate) {
        mapNameNumObj[mapName] = { mapName, num: item.num };
      }
    })
    let itemFood = null;
    let bmp = null;
    let wid = 0;
    let hei = 0;
    let matrix = null;
    _.forEach(mapNameNumObj, (item) => {
      itemFood = this.itemFoods[item.mapName];
      bmp = itemFood.children[0];
      wid = itemFood.nominalBounds.width;
      hei = itemFood.nominalBounds.height;
      //redMultiplier greenMultiplier blueMultiplier alphaMultiplier redOffset greenOffset blueOffset alphaOffset
      // bmp.filters = [new createjs.ColorFilter(0.9, 1, 1, 1, 0, 0, 0, 0)];
      if (this.injectJsClassObj.levelColors) {
        let { hue, saturation } = this.getHueSaturation(this.injectJsClassObj.levelColors, item);
        matrix = new window.createjs.ColorMatrix().adjustHue(hue).adjustSaturation(saturation);
        bmp.filters = [new window.createjs.ColorMatrixFilter(matrix)];
        bmp.cache(0, 0, wid, hei);
      }
      itemFood.visible = true;
    })
  }

  getHueSaturation(levelColors, item) {
    let levelKeys = _.keys(levelColors);
    let index = 0;
    let hue = 0;
    let saturation = 0;
    let hueSaturations = null;
    let levelColor = null;
    let maxKey = levelKeys[levelKeys.length - 1];
    if (item.num >= maxKey) {
      levelColor = levelColors[maxKey];
      hueSaturations = levelColor.split(",");
      hue = hueSaturations[0];
      saturation = hueSaturations[1];
    } else {
      for (let levelColor in levelColors) {
        if (item.num >= levelKeys[index] && item.num < levelKeys[index + 1]) {
          hueSaturations = levelColor.split(",");
          hue = hueSaturations[0];
          saturation = hueSaturations[1];
          break;
        }
        index++;
      }
    }
    return { hue, saturation };
  }

  destroy() {
    let item;
    while (this.items.length > 0) {
      item = this.items.pop();
      item.removeAllEventListeners();
    }
    this.items = null;
    this.itemFoods = null;
    this.stagePage = null;
    this.mapNameObjs = null;

    this.stage = null;
    this.stagePage = null;
    this.lib = null;
    this.item = null;
    this.commonAttr = null;
    this.store = null;
    this.injectJsClassObj = null;
    this.urls = null;
  }
}
